/*
//
//              INTEL CORPORATION PROPRIETARY INFORMATION
//  This software is supplied under the terms of a license  agreement or
//  nondisclosure agreement with Intel Corporation and may not be copied
//  or disclosed except in  accordance  with the terms of that agreement.
//    Copyright (c) 2007 Intel Corporation. All Rights Reserved.
//
//
*/

#ifndef __UMC_AVS_DEC_BYTE_STREAM_H
#define __UMC_AVS_DEC_BYTE_STREAM_H

#include "ippdefs.h"
#include "umc_structures.h"

namespace UMC
{

//
// This class was designed to parse headers only.
// Headers shall not have 'start code emulation preventing' bits,
// so we can use simplified bitstream parsing for them.
//

class AVSByteStream
{
public:
    // Default constructor
    AVSByteStream(void)
    {
        m_nBits = 0;
        m_iBitsNum = 0;

        m_pSrc = (const Ipp8u *) 0;
    }

    // Destructor
    ~AVSByteStream(void)
    {
        Close();
    }

    // Initialize the bit stream
    Status Init(void)
    {
        return UMC_OK;
    }

    // Set the source
    Status SetSource(const Ipp8u *pSource, size_t nSize)
    {
        m_nBits = 0;
        m_iBitsNum = 0;

        if (NULL == pSource)
            return UMC_ERR_NULL_PTR;

        m_pSrc = pSource;
        m_pSrcEnd = pSource + nSize;

        return UMC_OK;
    }

    // Set the source
    Status SetDestination(Ipp8u *pDestination, size_t nSize)
    {
        m_nBits = 0;
        m_iBitsNum = 32;

        if (NULL == pDestination)
            return UMC_ERR_NULL_PTR;

        m_pDst = pDestination;
        m_pDstEnd = pDestination + nSize;

        return UMC_OK;
    }

    // Release the object
    Status Close(void)
    {
        // check used amount of bit stream
        if ((m_pSrc < m_pSrcEnd) ||
            ((m_pSrc - m_pSrcEnd) * 8 <= m_iBitsNum))
            return UMC_OK;

        return UMC_ERR_FAILED;
    }

    // Get bit from the bit stream
    Ipp32u GetBit(void)
    {
        if (0 >= m_iBitsNum)
            LoadBits();

        m_iBitsNum -= 1;
        return ((m_nBits >> m_iBitsNum) & 1);
    }

    // Get bits from the bit stream
    Ipp32u GetBits(Ipp32s iNum)
    {
        if (24 < iNum)
            return 0;

        if (m_iBitsNum < iNum)
            LoadBits();

        m_iBitsNum -= iNum;
        return ((m_nBits >> m_iBitsNum) & ~(-1 << iNum));
    }

    // Skip some bits from the bitstream
    void SkipBits(Ipp32s iNum)
    {
        m_iBitsNum -= iNum;

        if (m_iBitsNum < iNum)
            LoadBits();
    }

    // Put bit to the bit stream
    void PutBit(Ipp32u bit)
    {
        if (0 >= m_iBitsNum)
            SaveBits();

        m_iBitsNum -= 1;
        m_nBits |= ((bit & 1) << m_iBitsNum);
    }

    // Put bits to the bit stream
    void PutBits(Ipp32u bits, Ipp32s iNum)
    {
        if (24 < iNum)
            return;

        if (m_iBitsNum < iNum)
            SaveBits();

        m_iBitsNum -= iNum;
        m_nBits |= ((bits & ~(-1 << iNum)) << m_iBitsNum);
    }

    // Flush the bitstream
    void Flush(void)
    {
        while (32 > m_iBitsNum)
        {
            m_pDst[0] = (Ipp8u) (m_nBits >> 24);
            m_nBits = m_nBits << 8;
            m_iBitsNum += 8;
            m_pDst += 1;
        }

        m_iBitsNum = 0;
    }

    inline
    Ipp8u *GetPointer(void)
    {
        return m_pDst;
    }

protected:
    // Upload bits
    void LoadBits(void)
    {
        while (24 >= m_iBitsNum)
        {
            m_nBits = (m_nBits << 8) | m_pSrc[0];
            m_iBitsNum += 8;
            m_pSrc += 1;
        }
    }

    // Download bits
    void SaveBits(void)
    {
        while (24 >= m_iBitsNum)
        {
            m_pDst[0] = (Ipp8u) (m_nBits >> 24);
            m_nBits = m_nBits << 8;
            m_iBitsNum += 8;
            m_pDst += 1;
        }
    }

    Ipp32u m_nBits;                                             // (Ipp32u) temporal register with bits
    Ipp32s m_iBitsNum;                                          // (Ipp32s) number of bits in the register

    union
    {
    const Ipp8u *m_pSrc;                                        // (const Ipp8u *) pointer to source data
    Ipp8u *m_pDst;                                              // (Ipp8u *) pointer to the destination buffer
    };
    union
    {
    const Ipp8u *m_pSrcEnd;                                     // (const Ipp8u *) pointer to the end of source data
    Ipp8u *m_pDstEnd;                                           // (Ipp8u *) pointer to the end of the destination buffer
    };
};

} // namespace UMC

#endif // __UMC_AVS_DEC_BYTE_STREAM_H
